package edu.xjdx.os.service.Impl;

import edu.xjdx.os.domain.*;
import edu.xjdx.os.mapper.*;
import edu.xjdx.os.service.CartService;
import edu.xjdx.os.utils.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private DiscountMapper discountMapper;

    @Override
    public void add(Customer customer, Goods goods, Integer amount) {
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andCustomerIdEqualTo(customer.getCustomerId());
        criteria.andGoodsIdEqualTo(goods.getGoodsId());
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        if (!carts.isEmpty()) {
            carts.get(0).setGoodsAmount((int) (carts.get(0).getGoodsAmount()) + amount);
            cartMapper.updateByPrimaryKeySelective(carts.get(0));
        } else {
            Cart cart = new Cart();
            cart.setCartId(IDGenerator.getId());
            cart.setGoodsId(goods.getGoodsId());
            cart.setCustomerId(customer.getCustomerId());
            cart.setGoodsAmount(amount);
            cartMapper.insert(cart);
        }
    }

    @Override
    public List<CartDetail> findByCustomer(Customer customer) {
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andCustomerIdEqualTo(customer.getCustomerId());
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        List<Dictionary> dictionaries = dictionaryMapper.findByName("商品类型");
        List<CartDetail> cartDetails = new ArrayList<>();
        for (Cart cart : carts) {
            CartDetail cartDetail = new CartDetail();
            cartDetail.setCartId(cart.getCartId());
            cartDetail.setCustomerId(cart.getCustomerId());
            cartDetail.setGoodsId(cart.getGoodsId());
            cartDetail.setGoodsAmount(cart.getGoodsAmount());
            Goods goods = goodsMapper.selectByPrimaryKey(cart.getGoodsId());
            cartDetail.setCustomer(customer);
            cartDetail.setGoods(goods);
            Double price = Double.parseDouble(goods.getGoodsPrice());
            Double total = price * cart.getGoodsAmount();
            Discount discount1=discountMapper.findByType(goods.getGoodsCategory());
            Calendar date=Calendar.getInstance();
            Calendar start=Calendar.getInstance();
            Calendar end=Calendar.getInstance();
            Date nowTime=new Date();
            date.setTime(nowTime);
            start.setTime(discount1.getStart());
            end.setTime(discount1.getEnd());
//            System.out.println(nowTime+" "+discount1.getStart()+" "+discount1.getEnd());
            Double discountPrice;
            if(date.before(end)&&date.after(start)){
                Double discount = Double.parseDouble(discount1.getAmount().toString())/100;
                discountPrice=total*discount;
//                System.out.println("yes"+total+" "+discountPrice);
            }else if (nowTime.compareTo(discount1.getStart()) == 0 || nowTime.compareTo(discount1.getEnd()) == 0) {
                Double discount = Double.parseDouble(discount1.getAmount().toString())/100;
                discountPrice=total*discount;
//                System.out.println("yes"+total+" "+discountPrice);
            }else {
                discountPrice=total;
//                System.out.println("no");
            }
            cartDetail.setTotal(total.toString());
            cartDetail.setDiscount(discountPrice.toString());
            cartDetails.add(cartDetail);
        }
        return cartDetails;
    }

    @Override
    public void modify(String id, Integer amount) {
        Cart cart = cartMapper.selectByPrimaryKey(id);
        cart.setGoodsAmount(amount);
        cartMapper.updateByPrimaryKey(cart);
    }

    @Override
    public void delete(String id) {
        cartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Cart findById(String id) {
        return cartMapper.selectByPrimaryKey(id);
    }
}
